# Create your views here.
# coding=utf-8
from django.template import Context, loader
from django.http import HttpResponse
from ltyxapp.models import Article,CommentSort,Comment
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.shortcuts import RequestContext

def hits(id):
    art=Article.objects.get(id=id)
    try:
        art.hits=int(art.hits)+1
    except:
        art.hits=1
    art.save()
    
def article(request,id1,id2):
    if request.method == 'GET':
        article=Article.objects.get(id=id2)
        hits(id2)
        
        sortname = '文档'
        sort = CommentSort.objects.get(sort=sortname)
        comment = Comment.objects.filter(commenttype=sort,to_id=id2)
        
        
        t = loader.get_template('articletail.html')
        c = Context({
                     'articles':article,
                     'comment':comment,
        })
        return HttpResponse(t.render(c))
    if request.method == 'POST':
        
        s = request.POST.get('sort')
        nickname = request.POST.get('nickname')
        content = request.POST.get('content')
        
        sort = CommentSort.objects.get(sort=s)
        
        Comment(commenttype=sort,
                to_id=id2,
                nickname=nickname,
                content=content).save()
                
        return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))

